/**
 * 客户管理
 * @author 牧羊人
 * @since 2021/12/21
 */
layui.use(['function','form', 'laydate', 'admin', 'zTree','element'], function () {
    //声明变量
    var func = layui.function
        , form = layui.form
        , admin = layui.admin
        , table = layui.table
        , element = layui.element
        $ = layui.$;
    if (A == 'index') {
        //【TABLE列数组】
        var cols = [
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', align: 'center', sort: true}
            , {field: 'name', width: 250, title: '公司名称', align: 'center'}
            , {field: 'fee_year', width: 150, title: '费减备案年份', align: 'center'}
            , {field: 'peer_str', width: 100, title: '同行公司', align: 'center'}
            , {field: 'general_number', width: 100, title: '总委编号', align: 'center'}
            , {field: 'address', width: 250, title: '地址', align: 'center'}
            , {field: 'service_str', width: 100, title: '业务员', align: 'center'}
            , {field: 'agent_str', width: 100, title: '代理人', align: 'center'}
            , {field: 'contacts_name', width: 100, title: '联系人', align: 'center'}
            , {field: 'phone', width: 100, title: '电话', align: 'center'}
            // , {field: 'create_user_name', width: 100, title: '创建人', align: 'center'}            
            , {fixed: 'right', width:250, title: '功能操作', align: 'center', toolbar: '#toolBar'}
        ];

        //【渲染TABLE】
        func.tableIns(cols, "tableList", function (layEvent, data) {
            if (layEvent === 'tab') {
                var url = cUrl + "/tab?company_id="+data.id;
                func.showWin("客户详情", url);
            }
        });
        

        //【设置弹框】
        func.setWin("客户", 1000, 700);
    }

    form.on('select(selectPid)', function (data) {   //选择并赋值给input框
        // value：data.value
        // 文本：data.elem[data.elem.selectedIndex].text;
        var txt = data.elem[data.elem.selectedIndex].text;
        $("#selectInput").val(txt);
        $("#selectPid").next().find("dl").css({ "display": "none" });
        form.render();
    });

    $(function(){
        search();
    })
    window.search = function (type='edit') {
        var value = $("#selectInput").val()??'',pid=$("#selectPid").attr("attr_pid");

        $.ajax({
           type:'GET',
           url:'getList',
           data:{"name":value},
           success:function(res){
                // 返回成功的数据
                var da = JSON.parse(res),str = '';
                if(da.code==0){
                    var option = '';  // option进行拼接
                    for(var i in da.data){
                        let select = '';
                        if(da.data[i]['id'] == pid && type == 'edit'){
                            select = "selected=true";
                            str = da.data[i]['name'];
                        }
                        option += "<option value='"+da.data[i]['id']+"' "+select+">"+da.data[i]['name']+"</option>";
                    }
                    if(type == 'edit'){
                        $("#selectInput").val(str)
                    }
                    $('#selectPid').html(option);　　// 给select节点追加
                    form.render('select')
                }
           }
        });

    }

    $("#batch_import").on("click",function(){
        var url = cUrl + "/../ofcompany/batch_import";
        func.showWin("批量导入", url,500,500);
    })

    $(".btn-save").on('click',function(){
        var formData = $("#form1").serialize();

        var name = $('input[name="name"]').val();
        name = name.replace(/(^\s*)|(\s*$)/g, "");
        if(!name){
            layer.msg("请填写客户名称");
            return;
        }
        
        $.ajax({
            url:'/ofcompany/edit',
            type:'post',
            data:formData,
            dataType:'json',
            success:function(data){

                if(data.code==0){
                    layer.msg(data.msg,{time:2000})

                    setTimeout(function(){
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                    },1000);

                    
                    parent.layui.table.reload('tableList');

                    // console.log(jian);
                    // table.reload();
                    // window.parent.location.reload();//刷新父页面
                    // var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    // parent.layer.close(index);
                }else{
                    layer.msg(data.msg, {time:2000});
                }
            }
        })
    })


    $('#download').on("click",function(e){
            // 选择数据
        // var checkStatus = table.checkStatus('tableList')
        //         , data = checkStatus.data,ids='';
        // if(data.length <= 0){
        //     ids = '';
        //     $msg = '当前未选中数据,将导出全部的客户';
        // }
        // for (da in data) {
        //     ids += ids==''?data[da].id:','+data[da].id;
        //     $msg = "当前选中"+data.length+"条数据导出";
        // }

        layer.confirm("即将导出",
           {icon: 3, title: '批量导出'}, 
           function (index) {
            layer.close(index);
            isDeploy=true;
             get_data()
        });
    })




    window.get_data = function ()
    {   
        var name = $("[name='name']").val();
        $.trim(name)
        window.location.href = '/ofcompany/download?name='+name;
    }
});
